package com.ruoyi.user.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.UserDataStatistics;
import com.ruoyi.user.service.IUserDataStatisticsService;

import cn.hutool.core.date.DateUtil;

/**
 * 用户数据统计Controller
 * 
 * @author DEREK
 * @date 2024-02-19
 */
@RestController
@RequestMapping("/user/statistics")
public class UserDataStatisticsController extends BaseController
{
    @Autowired
    private IUserDataStatisticsService userDataStatisticsService;

    /**
     * 	查询用户数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('user:statistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserDataStatistics userDataStatistics)
    {
    	if(null == userDataStatistics) {
    		userDataStatistics = new UserDataStatistics();
    		userDataStatistics.setStatisticsYear(DateUtil.format(new Date(), "yyyyMMdd"));
    	}
    	if(StringUtils.isEmpty(userDataStatistics.getStatisticsYear())) {
    		userDataStatistics.setStatisticsYear(DateUtil.format(new Date(), "yyyyMMdd"));
    	}
    	userDataStatistics.setStatisticsYear(userDataStatistics.getStatisticsYear().replace("-", ""));
    	userDataStatistics.setDayAdd(userDataStatistics.getStatisticsYear());
    	userDataStatistics.setWeekAdd(String.valueOf(Integer.valueOf(userDataStatistics.getStatisticsYear())-7));
    	userDataStatistics.setMonthAdd(userDataStatistics.getStatisticsYear().substring(0, 6));
        startPage();
        List<UserDataStatistics> list = userDataStatisticsService.selectUserDataStatisticsList(userDataStatistics);
        return getDataTable(list);
    }
    
    public static void main(String[] args) {
		System.out.println(Integer.valueOf("20240219")-7);
	}

    /**
     * 导出用户数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('user:statistics:export')")
    @Log(title = "用户数据统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserDataStatistics userDataStatistics)
    {
        List<UserDataStatistics> list = userDataStatisticsService.selectUserDataStatisticsList(userDataStatistics);
        ExcelUtil<UserDataStatistics> util = new ExcelUtil<UserDataStatistics>(UserDataStatistics.class);
        util.exportExcel(response, list, "用户数据统计数据");
    }

    /**
     * 获取用户数据统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:statistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userDataStatisticsService.selectUserDataStatisticsById(id));
    }

    /**
     * 新增用户数据统计
     */
    @PreAuthorize("@ss.hasPermi('user:statistics:add')")
    @Log(title = "用户数据统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserDataStatistics userDataStatistics)
    {
        return toAjax(userDataStatisticsService.insertUserDataStatistics(userDataStatistics));
    }

    /**
     * 修改用户数据统计
     */
    @PreAuthorize("@ss.hasPermi('user:statistics:edit')")
    @Log(title = "用户数据统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserDataStatistics userDataStatistics)
    {
        return toAjax(userDataStatisticsService.updateUserDataStatistics(userDataStatistics));
    }

    /**
     * 删除用户数据统计
     */
    @PreAuthorize("@ss.hasPermi('user:statistics:remove')")
    @Log(title = "用户数据统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userDataStatisticsService.deleteUserDataStatisticsByIds(ids));
    }
}
